package com.pp.controller;

import com.pp.entity.User;
import com.pp.service.LoginService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lanou on 18/2/1.
 */
@Controller
@RequestMapping("/")
public class LoginController {
    @Resource
    private LoginService loginService;


    @RequestMapping("index")
    public String index(){
        return "index";
    }

    @RequestMapping("login")
    @ResponseBody
    public Map<String,Object> login(User user, HttpSession session){
        Map<String,Object> map = new HashMap<>();
        User user1 = loginService.login(user);
        if(user1!=null){
            session.setAttribute("name",user1.getUserName());
            map.put("name",user1.getUserName());
        }
        return map;
    }
    @RequestMapping("exit")
    @ResponseBody
    public void exit(HttpSession session){
        session.invalidate();
    }
    @RequestMapping("register")
    @ResponseBody
    public void register(User user){
        loginService.register(user);
    }
    @RequestMapping("isRegister")
    @ResponseBody
    public boolean isRegister(User user){
        boolean isRegister = loginService.isRigister(user);
        if(isRegister){
            return true;
        }
        return false;
    }
    @RequestMapping("about")
    @ResponseBody
    public Map<String,Object> about(HttpSession session){
        Map<String,Object> map = new HashMap<>();
        String name = (String) session.getAttribute("name");
        String about = loginService.getUserAbout(name);
        map.put("about",about);
        return map;
    }
//    @RequestMapping("/game")
//    public String game(){
//        return "game";
//    }
}
